﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.BLL;
using Taurus.Platform.DTO;
using ComponentFactory.Krypton.Toolkit;
using Taurus.Parking.BLL;
using Taurus.Parking.DTO;

namespace Taurus.Parking.Trading
{
    public partial class TradingControl : UserControl
    {
        private AccountBll _account = new AccountBll();
        private AutomobileMarket _automobileMarket = new AutomobileMarket();
        private CarImage _ci = new CarImage();

        public TradingControl()
        {
            InitializeComponent();
        }

        private void TradingControl_Load(object sender, EventArgs e)
        {
            //绑定账号
            cbxAccount.DataSource = InitSys.Instance.MyGarageList;
            cbxAccount.DisplayMember = "Name";

            //绑定车市
            dgvAutomobileMarket.DataSource = _automobileMarket.GetAllCar();
        }

        private void cbxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            KryptonComboBox cbx = (KryptonComboBox)sender;
            //选中账号初始化该账号内容
            MyGarage myGargage = (MyGarage)cbx.SelectedItem;
            lblMyMoney.Text = myGargage.MyAccount.Cash + "元";
            dgvMyCars.DataSource = myGargage.MyAccount.Garage.MyCars;
            btnBuy.Enabled = true;
            btnSell.Enabled = true;
            lblSence.Values.ExtraText = myGargage.MyAccount.Garage.Scene;
            dgvMyPark.DataSource = myGargage.MyAccount.Garage.MyParks;
            headImg.Image = _account.GetAccountHeadImg(myGargage.MyAccount);
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            //手动更新车市资料
            _automobileMarket.UpdateAutombileMarketData();
        }

        private void kryptonButton2_Click(object sender, EventArgs e)
        {
           
            MyGarage myGargage = (MyGarage)cbxAccount.SelectedItem;
            if (myGargage.MyAccount.Garage.MyCars.Count>9)
            {
                MessageBox.Show("拥有车辆已经达到上限10辆", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //TODO：买
            }
           
        }

        private void kryptonButton3_Click(object sender, EventArgs e)
        {
            //TODO：卖
        }

        private void dgvAutomobileMarket_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            Car car = row.DataBoundItem as Car;
            if (car==null)
            {
                Park p = (Park)row.DataBoundItem;
                car = p.Car;
            }
            picBoxCar.Image = _ci.GetImage(car);
            lblCarName.Text = "名称：" + car.Name;
            lblCarPrice.Text = "价格：" + car.Price + "元";
        }

        private void dgvMyCars_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            
            if (grid.Columns[e.ColumnIndex].HeaderText == "停在谁家")
            {
                Car car = (Car)grid.Rows[e.RowIndex].DataBoundItem;
                e.Value = car.Park.OwnerName;

            }
            else if (grid.Columns[e.ColumnIndex].HeaderText == "预计收入")
            {
                Car car = (Car)grid.Rows[e.RowIndex].DataBoundItem;
                e.Value = car.Park.Profit;
            }
        }

        private void dgvMyPark_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            if (grid.Columns[e.ColumnIndex].HeaderText == "谁的汽车")
            {
                Park park = (Park)grid.Rows[e.RowIndex].DataBoundItem;
                e.Value = park.Car.OwnerName;
            }
            else if (grid.Columns[e.ColumnIndex].HeaderText == "车名")
            {
                Park park = (Park)grid.Rows[e.RowIndex].DataBoundItem;
                e.Value = park.Car.Name;
            }
            else if (grid.Columns[e.ColumnIndex].HeaderText == "车的价格")
            {
                Park park = (Park)grid.Rows[e.RowIndex].DataBoundItem;
                e.Value = park.Car.Price;
            }
        }
    }
}
